<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>BrokenIRC: server_src/server_init.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">BrokenIRC
   &#160;<span id="projectnumber">0.0.42</span>
   </div>
   <div id="projectbrief">A &#39;not so functionnal&#39; irc server and a WIP client</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_20e5c1c74b8b955148e6b35ac4a0bc4d.html">server_src</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">server_init.c File Reference</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &quot;<a class="el" href="server_8h_source.html">../include/server.h</a>&quot;</code><br />
<code>#include &lt;unistd.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;fcntl.h&gt;</code><br />
<code>#include &lt;sys/types.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for server_init.c:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c__incl.png" border="0" usemap="#server__src_2server__init_8c" alt=""/></div>
<map name="server__src_2server__init_8c" id="server__src_2server__init_8c">
<area shape="rect" id="node2" href="server_8h.html" title="../include/server.h" alt="" coords="150,80,277,107"/>
</map>
</div>
</div>
<p><a href="server__init_8c_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:aa9bc3ff8e7f409540f85b3cc97636500"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="server__init_8c.html#aa9bc3ff8e7f409540f85b3cc97636500">set_clifd</a> (int clisock, int epollfd, struct epoll_event *ev)</td></tr>
<tr class="separator:aa9bc3ff8e7f409540f85b3cc97636500"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3802a71fa34e93cb1d85f919914f5abf"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="server__init_8c.html#a3802a71fa34e93cb1d85f919914f5abf">set_iface</a> (<a class="el" href="server_8h.html#aa62dc6ab378205892df275c6df156dba">adrinf</a> *hints, <a class="el" href="server_8h.html#aa62dc6ab378205892df275c6df156dba">adrinf</a> **res, const char *port)</td></tr>
<tr class="separator:a3802a71fa34e93cb1d85f919914f5abf"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a5cb15d408ef32c09c2485937503438ed"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="server__init_8c.html#a5cb15d408ef32c09c2485937503438ed">set_epoll</a> (<a class="el" href="structt__serv.html">t_serv</a> *all)</td></tr>
<tr class="separator:a5cb15d408ef32c09c2485937503438ed"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a381b88f843ed1ebdaa5b2a8ded74931b"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="server__init_8c.html#a381b88f843ed1ebdaa5b2a8ded74931b">prevent_reused</a> (int sockfd)</td></tr>
<tr class="separator:a381b88f843ed1ebdaa5b2a8ded74931b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a1f61d16ccfab82b21ca40ce23feb7c02"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="server__init_8c.html#a1f61d16ccfab82b21ca40ce23feb7c02">set_sockfd</a> (<a class="el" href="structt__serv.html">t_serv</a> *all)</td></tr>
<tr class="separator:a1f61d16ccfab82b21ca40ce23feb7c02"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<h2 class="groupheader">Function Documentation</h2>
<a id="a381b88f843ed1ebdaa5b2a8ded74931b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a381b88f843ed1ebdaa5b2a8ded74931b">&#9670;&nbsp;</a></span>prevent_reused()</h2>

<div class="memitem">
<div class="memproto">
<table class="mlabels">
  <tr>
  <td class="mlabels-left">
      <table class="memname">
        <tr>
          <td class="memname">static void prevent_reused </td>
          <td>(</td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>sockfd</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
  </td>
  <td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span>  </td>
  </tr>
</table>
</div><div class="memdoc">
<p>Prevents those dreaded "Address already in use" errors </p>

<p class="definition">Definition at line <a class="el" href="server__init_8c_source.html#l00082">82</a> of file <a class="el" href="server__init_8c_source.html">server_init.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="server__init_8c_source.html#l00100">set_sockfd()</a>.</p>
<div class="fragment"><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;{</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;        <span class="keywordtype">int</span> yes = 1;</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;        <span class="keywordflow">if</span> (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (<span class="keyword">const</span> <span class="keywordtype">void</span> *)&amp;yes,</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;                <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)) == -1) {</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;                perror(<span class="stringliteral">&quot;prevent_reused: setsockopt&quot;</span>);</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;                exit(84);</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;        }</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;}</div></div><!-- fragment --><div class="dynheader">
Here is the caller graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_a381b88f843ed1ebdaa5b2a8ded74931b_icgraph.png" border="0" usemap="#server__init_8c_a381b88f843ed1ebdaa5b2a8ded74931b_icgraph" alt=""/></div>
<map name="server__init_8c_a381b88f843ed1ebdaa5b2a8ded74931b_icgraph" id="server__init_8c_a381b88f843ed1ebdaa5b2a8ded74931b_icgraph">
<area shape="rect" id="node2" href="server_8h.html#a1f61d16ccfab82b21ca40ce23feb7c02" title="set_sockfd" alt="" coords="164,5,251,32"/>
<area shape="rect" id="node3" href="server__main_8c.html#af3e041546aaed3cd242c2b0e77162b39" title="server" alt="" coords="299,5,357,32"/>
<area shape="rect" id="node4" href="server__main_8c.html#a0c99d968a34e803d378692bde2e3f18f" title="main" alt="" coords="405,5,456,32"/>
</map>
</div>

</div>
</div>
<a id="aa9bc3ff8e7f409540f85b3cc97636500"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa9bc3ff8e7f409540f85b3cc97636500">&#9670;&nbsp;</a></span>set_clifd()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int set_clifd </td>
          <td>(</td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>clisock</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int&#160;</td>
          <td class="paramname"><em>epollfd</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">struct epoll_event *&#160;</td>
          <td class="paramname"><em>ev</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Add the new client file descriptor in struct epoll_event structure for polling their events  read <a href="https://bit.ly/2gOr7gr">this</a> medium blog post on 'epoll madness' </p>

<p class="definition">Definition at line <a class="el" href="server__init_8c_source.html#l00018">18</a> of file <a class="el" href="server__init_8c_source.html">server_init.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="server__main_8c_source.html#l00020">initco()</a>.</p>
<div class="fragment"><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;{</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        <span class="keywordtype">int</span> rt = 0;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        <span class="keywordtype">int</span> op = EPOLL_CTL_ADD;</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;        ev-&gt;events = EPOLLIN | EPOLLET;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;        ev-&gt;data.fd = clisock;</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;        <span class="keywordflow">if</span> (epoll_ctl(epollfd, op, clisock, ev) == -1) {</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;                perror(<span class="stringliteral">&quot;set_clifd: epoll_ctl:&quot;</span>);</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;                rt = 84;</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;        }</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        fprintf(stderr, <span class="stringliteral">&quot;Add client n°[%d] to epoll interest list\n&quot;</span>, clisock);</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;        <span class="keywordflow">return</span> (rt);</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;}</div></div><!-- fragment --><div class="dynheader">
Here is the caller graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_aa9bc3ff8e7f409540f85b3cc97636500_icgraph.png" border="0" usemap="#server__init_8c_aa9bc3ff8e7f409540f85b3cc97636500_icgraph" alt=""/></div>
<map name="server__init_8c_aa9bc3ff8e7f409540f85b3cc97636500_icgraph" id="server__init_8c_aa9bc3ff8e7f409540f85b3cc97636500_icgraph">
<area shape="rect" id="node2" href="server__main_8c.html#a01aa22f887842d97133af9b3f2cf4a7c" title="initco" alt="" coords="124,5,177,32"/>
<area shape="rect" id="node3" href="server__main_8c.html#a825c9cedc1751b000c44487b4233c37c" title="getactiveclients" alt="" coords="225,5,339,32"/>
<area shape="rect" id="node4" href="server__main_8c.html#af3e041546aaed3cd242c2b0e77162b39" title="server" alt="" coords="387,5,445,32"/>
<area shape="rect" id="node5" href="server__main_8c.html#a0c99d968a34e803d378692bde2e3f18f" title="main" alt="" coords="493,5,544,32"/>
</map>
</div>

</div>
</div>
<a id="a5cb15d408ef32c09c2485937503438ed"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a5cb15d408ef32c09c2485937503438ed">&#9670;&nbsp;</a></span>set_epoll()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int set_epoll </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="structt__serv.html">t_serv</a> *&#160;</td>
          <td class="paramname"><em>all</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Epoll IO handler initialisation</p><ul>
<li>declare an epoll fd with epoll_create()</li>
<li>set event handling to EPOLLIN (check <code>man epoll</code>)</li>
<li>register server listening fd on ev structure </li>
</ul>

<p class="definition">Definition at line <a class="el" href="server__init_8c_source.html#l00059">59</a> of file <a class="el" href="server__init_8c_source.html">server_init.c</a>.</p>

<p class="reference">References <a class="el" href="server_8h_source.html#l00087">t_serv::epollfd</a>, <a class="el" href="server_8h_source.html#l00097">t_serv::ev</a>, and <a class="el" href="server_8h_source.html#l00089">t_serv::listen_sock</a>.</p>

<p class="reference">Referenced by <a class="el" href="server__main_8c_source.html#l00096">server()</a>.</p>
<div class="fragment"><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;{</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;        <span class="keywordtype">int</span> rt = 0;</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;        all-&gt;<a class="code" href="structt__serv.html#a46bee037324e9deec26f15fbc4f6d8e5">epollfd</a> = epoll_create(10);</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;        memset(&amp;all-&gt;<a class="code" href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">ev</a>, 0, <span class="keyword">sizeof</span>(all-&gt;<a class="code" href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">ev</a>));</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;        <span class="keywordflow">if</span> (all-&gt;<a class="code" href="structt__serv.html#a46bee037324e9deec26f15fbc4f6d8e5">epollfd</a> == -1) {</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;                perror(<span class="stringliteral">&quot;set_epoll: epoll_create1&quot;</span>);</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;                rt = 84;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        } <span class="keywordflow">else</span> {</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;                all-&gt;<a class="code" href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">ev</a>.events = EPOLLIN;</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;                all-&gt;<a class="code" href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">ev</a>.data.fd = all-&gt;<a class="code" href="structt__serv.html#ac9f24d41d8822f2c1b7df4362e18cad9">listen_sock</a>;</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;                rt = epoll_ctl(all-&gt;<a class="code" href="structt__serv.html#a46bee037324e9deec26f15fbc4f6d8e5">epollfd</a>, EPOLL_CTL_ADD, all-&gt;<a class="code" href="structt__serv.html#ac9f24d41d8822f2c1b7df4362e18cad9">listen_sock</a>,</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;                        &amp;all-&gt;<a class="code" href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">ev</a>);</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;                <span class="keywordflow">if</span> (rt == -1) {</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;                        perror(<span class="stringliteral">&quot;set_epoll: epoll_ctl&quot;</span>);</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;                        rt = 84;</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;                }</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;        }</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;        <span class="keywordflow">return</span> (rt);</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;}</div><div class="ttc" id="structt__serv_html_ac9f24d41d8822f2c1b7df4362e18cad9"><div class="ttname"><a href="structt__serv.html#ac9f24d41d8822f2c1b7df4362e18cad9">t_serv::listen_sock</a></div><div class="ttdeci">int listen_sock</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00089">server.h:89</a></div></div>
<div class="ttc" id="structt__serv_html_a46bee037324e9deec26f15fbc4f6d8e5"><div class="ttname"><a href="structt__serv.html#a46bee037324e9deec26f15fbc4f6d8e5">t_serv::epollfd</a></div><div class="ttdeci">int epollfd</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00087">server.h:87</a></div></div>
<div class="ttc" id="structt__serv_html_aa95442079d9e7a2294ad8df1335ae0f4"><div class="ttname"><a href="structt__serv.html#aa95442079d9e7a2294ad8df1335ae0f4">t_serv::ev</a></div><div class="ttdeci">struct epoll_event ev</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00097">server.h:97</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the caller graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_a5cb15d408ef32c09c2485937503438ed_icgraph.png" border="0" usemap="#server__init_8c_a5cb15d408ef32c09c2485937503438ed_icgraph" alt=""/></div>
<map name="server__init_8c_a5cb15d408ef32c09c2485937503438ed_icgraph" id="server__init_8c_a5cb15d408ef32c09c2485937503438ed_icgraph">
<area shape="rect" id="node2" href="server__main_8c.html#af3e041546aaed3cd242c2b0e77162b39" title="server" alt="" coords="128,5,187,32"/>
<area shape="rect" id="node3" href="server__main_8c.html#a0c99d968a34e803d378692bde2e3f18f" title="main" alt="" coords="235,5,285,32"/>
</map>
</div>

</div>
</div>
<a id="a3802a71fa34e93cb1d85f919914f5abf"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3802a71fa34e93cb1d85f919914f5abf">&#9670;&nbsp;</a></span>set_iface()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int set_iface </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="server_8h.html#aa62dc6ab378205892df275c6df156dba">adrinf</a> *&#160;</td>
          <td class="paramname"><em>hints</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="server_8h.html#aa62dc6ab378205892df275c6df156dba">adrinf</a> **&#160;</td>
          <td class="paramname"><em>res</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const char *&#160;</td>
          <td class="paramname"><em>port</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Listening interface setup </p>

<p class="definition">Definition at line <a class="el" href="server__init_8c_source.html#l00034">34</a> of file <a class="el" href="server__init_8c_source.html">server_init.c</a>.</p>

<p class="reference">Referenced by <a class="el" href="server__main_8c_source.html#l00096">server()</a>.</p>
<div class="fragment"><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;{</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;        <span class="keywordtype">int</span> rt = 0;</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;        memset(hints, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> addrinfo));</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        hints-&gt;ai_family = AF_UNSPEC;</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;        hints-&gt;ai_socktype = SOCK_STREAM;</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;        hints-&gt;ai_flags = AI_PASSIVE;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;        hints-&gt;ai_protocol = IPPROTO_TCP;</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;        hints-&gt;ai_canonname = NULL;</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;        hints-&gt;ai_addr = NULL;</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;        hints-&gt;ai_next = NULL;</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;        <span class="keywordflow">if</span> ((getaddrinfo(NULL, port, hints, res))) {</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;                perror(<span class="stringliteral">&quot;set_iface: getaddrinfo&quot;</span>);</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;                rt = 84;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;        }</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        <span class="keywordflow">return</span> (rt);</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;}</div></div><!-- fragment --><div class="dynheader">
Here is the caller graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_a3802a71fa34e93cb1d85f919914f5abf_icgraph.png" border="0" usemap="#server__init_8c_a3802a71fa34e93cb1d85f919914f5abf_icgraph" alt=""/></div>
<map name="server__init_8c_a3802a71fa34e93cb1d85f919914f5abf_icgraph" id="server__init_8c_a3802a71fa34e93cb1d85f919914f5abf_icgraph">
<area shape="rect" id="node2" href="server__main_8c.html#af3e041546aaed3cd242c2b0e77162b39" title="server" alt="" coords="128,5,187,32"/>
<area shape="rect" id="node3" href="server__main_8c.html#a0c99d968a34e803d378692bde2e3f18f" title="main" alt="" coords="235,5,285,32"/>
</map>
</div>

</div>
</div>
<a id="a1f61d16ccfab82b21ca40ce23feb7c02"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a1f61d16ccfab82b21ca40ce23feb7c02">&#9670;&nbsp;</a></span>set_sockfd()</h2>

<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">int set_sockfd </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="structt__serv.html">t_serv</a> *&#160;</td>
          <td class="paramname"><em>all</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Tcp Listening socket setup</p><ul>
<li>declare a socket with socket()</li>
<li>bind() to an interface</li>
<li>listen() on this fd / socket <dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">all</td><td>the 'foure tout' structure containing the struct to setup </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>sfd the configured listening socket fd or -1 if failure </dd></dl>
</li>
</ul>

<p class="definition">Definition at line <a class="el" href="server__init_8c_source.html#l00100">100</a> of file <a class="el" href="server__init_8c_source.html">server_init.c</a>.</p>

<p class="reference">References <a class="el" href="server_8h_source.html#l00020">BACKLOG</a>, <a class="el" href="server__init_8c_source.html#l00082">prevent_reused()</a>, <a class="el" href="server_8h_source.html#l00095">t_serv::res</a>, and <a class="el" href="server_8h_source.html#l00094">t_serv::rp</a>.</p>

<p class="reference">Referenced by <a class="el" href="server__main_8c_source.html#l00096">server()</a>.</p>
<div class="fragment"><div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;{</div><div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;        <span class="keywordtype">int</span> sfd = -1;</div><div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div><div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;        <span class="keywordflow">for</span> (all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a> = all-&gt;<a class="code" href="structt__serv.html#ae8783e38ef0c183366703267ca7b3298">res</a>; all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a> != NULL; all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a> = all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_next) {</div><div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;                sfd = socket(all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_family, all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_socktype,</div><div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;                        all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_protocol);</div><div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;                <a class="code" href="server__init_8c.html#a381b88f843ed1ebdaa5b2a8ded74931b">prevent_reused</a>(sfd);</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;                <span class="keywordflow">if</span> (sfd == -1) {</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;                        <span class="keywordflow">continue</span>;</div><div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;                }</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;                <span class="keywordflow">if</span> (bind(sfd, all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_addr, all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a>-&gt;ai_addrlen) == 0) {</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;                        <span class="keywordflow">break</span>;</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;                }</div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;                close(sfd);</div><div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;        }</div><div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;        <span class="keywordflow">if</span> (all-&gt;<a class="code" href="structt__serv.html#a018d976aed4b9930a794da25a758a024">rp</a> == NULL) {</div><div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;                fprintf(stderr, <span class="stringliteral">&quot;Could not bind\n&quot;</span>);</div><div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;        } <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((listen(sfd, <a class="code" href="server_8h.html#aeefbbafa97642defe3ee6c3080b7d66f">BACKLOG</a>) == -1)) {</div><div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;                perror(<span class="stringliteral">&quot;listen: set_sockfd&quot;</span>);</div><div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;        }</div><div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;        <span class="keywordflow">return</span> (sfd);</div><div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;}</div><div class="ttc" id="server_8h_html_aeefbbafa97642defe3ee6c3080b7d66f"><div class="ttname"><a href="server_8h.html#aeefbbafa97642defe3ee6c3080b7d66f">BACKLOG</a></div><div class="ttdeci">#define BACKLOG</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00020">server.h:20</a></div></div>
<div class="ttc" id="structt__serv_html_a018d976aed4b9930a794da25a758a024"><div class="ttname"><a href="structt__serv.html#a018d976aed4b9930a794da25a758a024">t_serv::rp</a></div><div class="ttdeci">adrinf * rp</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00094">server.h:94</a></div></div>
<div class="ttc" id="server__init_8c_html_a381b88f843ed1ebdaa5b2a8ded74931b"><div class="ttname"><a href="server__init_8c.html#a381b88f843ed1ebdaa5b2a8ded74931b">prevent_reused</a></div><div class="ttdeci">static void prevent_reused(int sockfd)</div><div class="ttdef"><b>Definition:</b> <a href="server__init_8c_source.html#l00082">server_init.c:82</a></div></div>
<div class="ttc" id="structt__serv_html_ae8783e38ef0c183366703267ca7b3298"><div class="ttname"><a href="structt__serv.html#ae8783e38ef0c183366703267ca7b3298">t_serv::res</a></div><div class="ttdeci">adrinf * res</div><div class="ttdef"><b>Definition:</b> <a href="server_8h_source.html#l00095">server.h:95</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_cgraph.png" border="0" usemap="#server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_cgraph" alt=""/></div>
<map name="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_cgraph" id="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_cgraph">
<area shape="rect" id="node2" href="server__init_8c.html#a381b88f843ed1ebdaa5b2a8ded74931b" title="prevent_reused" alt="" coords="140,5,251,32"/>
</map>
</div>
<div class="dynheader">
Here is the caller graph for this function:</div>
<div class="dyncontent">
<div class="center"><img src="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_icgraph.png" border="0" usemap="#server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_icgraph" alt=""/></div>
<map name="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_icgraph" id="server__init_8c_a1f61d16ccfab82b21ca40ce23feb7c02_icgraph">
<area shape="rect" id="node2" href="server__main_8c.html#af3e041546aaed3cd242c2b0e77162b39" title="server" alt="" coords="140,5,199,32"/>
<area shape="rect" id="node3" href="server__main_8c.html#a0c99d968a34e803d378692bde2e3f18f" title="main" alt="" coords="247,5,297,32"/>
</map>
</div>

</div>
</div>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
